﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BISPOS.Repository;
using DevExpress.XtraEditors.Controls;
namespace BISPOS
{
    public partial class ProductDetailsUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        ProductRepository repository = new ProductRepository();
        UIHelper helper = new UIHelper();
        Product _product = new Product();
        public void ReloadPrice()
        {
            priceGroupControl.Controls.Clear();
            foreach (var price in repository.Get(_product.Id).Prices)
            {
                PriceDetailsUserControl priceControl = new PriceDetailsUserControl(price);
                priceGroupControl.Controls.Add(priceControl);
                priceControl.Dock = DockStyle.Top;
            }
        }
        public ProductDetailsUserControl(Product product)
        {
            InitializeComponent();
            productBindingSource.DataSource = product;
            if (product.CapitalCost != 0)
                capitalPriceTextEdit.Text = product.CapitalCost.ToString("n2");
            else
                capitalPriceTextEdit.Text = "ไม่พบการซื้อ";
            helper.FillProductGroups(this.productGroupsImageComboBoxEdit, product.ProductGroupId);
            foreach (var price in product.Prices)
            {
                PriceDetailsUserControl priceControl = new PriceDetailsUserControl(price);
                priceGroupControl.Controls.Add(priceControl);
                priceControl.Dock = DockStyle.Top;
            }
            _product = product;

            // initialize
            // end
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if (editButton.Text == "แก้ไข")
            {
                saveButton.Enabled = true;
                editButton.Text = "ยกเลิก";
                nameTextEdit.Properties.ReadOnly = false;
                unitTextEdit.Properties.ReadOnly = false;
                productGroupsImageComboBoxEdit.Properties.ReadOnly = false;
                isTaxedCheckEdit.Properties.ReadOnly = false;
            }
            else
            {
                editButton.Text = "แก้ไข";
                saveButton.Enabled = false;
                nameTextEdit.Properties.ReadOnly = true;
                unitTextEdit.Properties.ReadOnly = true;
                productGroupsImageComboBoxEdit.Properties.ReadOnly = true;
                isTaxedCheckEdit.Properties.ReadOnly = true;
            }

        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                repository.EditProduct(barcodeTextEdit.Text, nameTextEdit.Text,
                            unitTextEdit.Text,
                            (int)productGroupsImageComboBoxEdit.EditValue,
                            (bool)isTaxedCheckEdit.EditValue);
                nameTextEdit.Properties.ReadOnly = false;
                unitTextEdit.Properties.ReadOnly = false;
                productGroupsImageComboBoxEdit.Properties.ReadOnly = false;
                isTaxedCheckEdit.Properties.ReadOnly = false;
                
                saveButton.Enabled = false;
                editButton.Text = "แก้ไข";

                nameTextEdit.Properties.ReadOnly = true;
                unitTextEdit.Properties.ReadOnly = true;
                productGroupsImageComboBoxEdit.Properties.ReadOnly = true;
                isTaxedCheckEdit.Properties.ReadOnly = true;

            }
            catch { }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            AddPriceForm form = new AddPriceForm(_product,this);
            form.Show();
           
        }
    }
}
